<template>
  <div class="login-contalner">
    <div class="login">
      <!-- 头像区域 -->
      <div class="avatar">
        <img src='../assets/dogzi.jpg' alt="">
      </div>
      <!-- 表单区域 -->
      <!-- 用户名 -->
    <el-form ref="loginForm" class="login-form" :model="loginForm" :rules="loginRules">
      <el-form-item prop="username">
        <el-input  prefix-icon="iconfont icon-user" v-model='loginForm.username'></el-input>
     </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" prefix-icon="iconfont icon-lock_fill" @keyup.enter.native ="login"  v-model='loginForm.password'></el-input>
     </el-form-item>
     <el-form-item class="login-btn">
        <el-button type="primary"   @click="login"  class="btns">登陆</el-button>
        <el-button type="info" @click="resetForm" >重置</el-button>
     </el-form-item>
   </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 登陆表单对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 定义校验规则
      loginRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      // console.log(this.$refs)
      this.$refs.loginForm.resetFields()
    },
    login () {
      this.$refs.loginForm.validate(async valid => {
        if (!valid) return
        // 要求传入username 和 password 可以直接将this.loginForm作为参数传递到服务器
        // 解构返回结果,并重新命名为res   冒号是重新命名的作用
        const { data: res } = await this.$axios.post('login', this.loginForm)
        // console.log(res)
        if (res.meta.status !== 200) return this.$message.error('用户名或密码错误!')
        this.$message.success(res.meta.msg)
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>
// scoped 表示为局部样式
<style scoped lang="less">
.login-contalner{
  position: relative;
  width: 100%;
  height: 100%;
  background: url('../assets/loginbg.jpg') no-repeat;
  background-size: 100%;
  .login{
    position:absolute;
    width: 450px;
    height: 350px;
    background-color: rgba(0,0,0,0.5);
    border-radius: 5px;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%);
    .avatar{
      position:absolute;
      left:50%;
      width: 130px;
      height: 130px;
      border-radius: 50%;
      text-align: center;
      transform:translate(-50%,-50%);
      box-shadow: 0 0 10px 0 #000;
      animation: le .7s linear;
      transition: all 0.3s;
      overflow:hidden;
      img{
        width: 100%;
        height: 100%;
        // margin-top: 20px;
        // border-radius:50%;
      }
    }
  }
}
.avatar:hover{
  box-shadow: 0 0 10px 0 #fff !important;
}
@keyframes le{
  0%{
     transform:translate(-50%,0%);
  }
  100%{
     transform:translate(-50%,-50%);
  }
}
@keyframes le1{
  0%{
     transform:translate(0%,20%);
  }
  100%{
     transform:translate(0%,0%);
  }
}

.login-form{
  // display: flex;
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  box-sizing: border-box;
  padding: 20px;
  animation: le1 .7s linear;
}
.login-btn{
  display: flex;
  justify-content: flex-end;
}
</style>
